package com.example.test_app;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.ConcatAdapter;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.test_app.adapter.ImageAdapter;
import com.example.test_app.adapter.TextAdapter;
import com.example.test_app.adapter.ViewPageAdapter;
import com.example.test_app.databinding.ActivityRvBinding;
import com.example.test_app.livedata.LiveDataBus;
import com.example.test_app.view.LivePagerLayoutManager;
import com.example.test_app.view.MyLayoutManager;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author a
 */
public class RvActivity extends AppCompatActivity {
    ActivityRvBinding binding;
    ViewPageAdapter viewPageAdapter;
    TextAdapter textAdapter;
    ImageAdapter imageAdapter;
    private List<String> data0 = new ArrayList<>()
            , data1 = new ArrayList<>();
    private List<List<Fragment>> data=new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityRvBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        for (int i = 0; i < 10; i++) {
            List<Fragment> list=new ArrayList<>();
            for (int j = 0; j <3 ; j++) {
                list.add(new BlankFragment2());
            }
            data.add(list);
        }
        for (int i = 0; i < 50; i++) {
            data0.add("text " + i);
            data1.add("img " + i);
        }
        viewPageAdapter=new ViewPageAdapter(this,data,getSupportFragmentManager());
        textAdapter = new TextAdapter(this, data0);
        imageAdapter = new ImageAdapter(this, data1);

        ConcatAdapter concatAdapter = new ConcatAdapter();
        concatAdapter.addAdapter(viewPageAdapter);
//        concatAdapter.addAdapter(textAdapter);
//        concatAdapter.addAdapter(imageAdapter);

//        binding.rvTest.setLayoutManager(new LivePagerLayoutManager(this));
        binding.rvTest.setLayoutManager(new LivePagerLayoutManager(this));
        binding.rvTest.setAdapter(concatAdapter);
        binding.smrl.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                data0.add(0, "新的数据");
                imageAdapter.notifyDataSetChanged();
                refreshLayout.finishRefresh();

            }
        });
        LiveDataBus.get().with("1",String.class).addObserverForSticky(this.getLifecycle(),new com.example.test_app.livedata.Observer<String>() {
            @Override
            public void onchange(String s) {
                Toast.makeText(RvActivity.this,s,Toast.LENGTH_LONG).show();
            }
        });
    }

    public void addData(View view) {
        binding.smrl.autoRefresh();
    }
}